<?php

namespace App\Controller\System;

use App\Controller\ApiController;
use function GuzzleHttp\Psr7\str;
use Usap\Service\Service;
use Think\Cache;


class FrontPageController extends ApiController
{
    /*
       *
       * 设备坐标信息
       *
       *  */
    public function getPosition()
    {
        $device_service = Service::instance()->find('devices', 'devices');
        //获取设备类别配置
        $device_categroy_config = $device_service->getDeviceCategoryConfig();
        $where = 1;
        $page = I('request.page') ? I('request.page') : 1;
        $rows = I('request.rows') ? I('request.rows') : '';
        $type = I('request.type') ? I('request.type') : 1;
        if ($type == 2) {
            $where .= " and device_category ='11'";
        } elseif ($type == 3) {
            $where .= " and device_category ='12'";
        } elseif ($type == 4) {
            $where .= " and device_category ='10'";
        } elseif ($type == 5) {
            $where .= " and is_alarming ='1'";
        }
        $device_list = $device_service->getDeviceList($where, $page, $rows);
        $device_info = [];
        if ($device_list['list']) {
            $online = $alert = $wen = $qi = $yan = $standby = $total = 0;
            foreach ($device_list['list'] as $value) {
                $total++;
                if ($value['is_alarming'] == 1) {
                    $alert++;
                }
                if ($value['device_category'] == 10) {
                    $qi++;
                } elseif ($value['device_category'] == 11) {//
                    $yan++;
                } else {
                    $wen++;
                }
                if ($value['is_online'] == 1) {
                    $online++;
                }
                if ($value['is_standby'] == 1) {
                    $standby++;
                }
                $res['id'] = $value['device_id'];
                $res['codeserial'] = $value['codeserial'];
                $res['device_category'] = $device_categroy_config[$value['device_category']];
                $res['device_address'] = $value['device_address'];
                $res['is_standby'] = $value['is_standby'];
                $res['is_alarming'] = $value['is_alarming'];
                $res['position'] = $value['position'] ? $value['position'] : '113.924535,22.571978';
                $device_info['info'][] = $res;
                $device_info['count'] = $device_list['count'];
                $device_info['page'] = $page;
                $device_info['total'] = ceil($device_list['count'] / 4);
            }
            $device_info['base'] = array(
                'total' => $total, 'yan' => $yan, 'qi' => $qi, 'wen' => $wen, 'alert' => $alert, 'online' => $online, 'standby' => $standby
            );
        }
        $this->ajaxReturn($device_info);
    }


    /**
     * 获取最新警报信息
     */
    public function alertInfo()
    {
        $alarms_service = Service::instance()->find('common', 'alarms');
        $devices_service = Service::instance()->find('devices', 'devices');
        $alarm_type_config = Service::instance()->loadConfig('common', 'common', 'alarmsTypeConfig');
        $device_categroy_config = $devices_service->getDeviceCategoryConfig();
        $where = 'a.status =0';
        $alarms_info = $alarms_service->getAlarmsList($where);
        $res = [];
        foreach ($alarms_info['list'] as $value) {
            $res[$value['codeserial']][] = $value;
        }
        $list = [];
        if ($res) {
            foreach ($res as $k => $v) {
                if (count($v) > 1) {
                    $new_alarm = array_pop($v);
                } else {
                    $new_alarm = $v[0];
                }
                $codeserial = $k;
                $device_info = $devices_service->getInfoByCodeserial($codeserial);
                if (!$device_info) {
                    continue;
                }
                $r['alarm_times'] = count($v);
                $r['created_time'] = date('Y-m-d H:i:s', $new_alarm['created_time']);
                $r['alarm_type'] = $alarm_type_config[$new_alarm['type']];
                $r['device_id'] = $device_info['device_id'];
                $r['device_category'] = $device_categroy_config[$device_info['device_category']];
                $r['codeserial'] = $device_info['codeserial'];
                $r['device_address'] = $device_info['device_address'];
                $list[] = $r;
            }
        }
        if ($list) {
            $this->ajaxReturn(array('code' => 1, 'info' => $list));
        } else {
            $this->ajaxReturn(array('code' => 0, 'info' => $list));
        }
    }

    /**
     * 获取最新警报信息
     */
    public function NewalertInfo()
    {
        $alarms_service = Service::instance()->find('common', 'alarms');
        $devices_service = Service::instance()->find('devices', 'devices');
        $alarm_type_config = Service::instance()->loadConfig('common', 'common', 'alarmsTypeConfig');
        $device_categroy_config = $devices_service->getDeviceCategoryConfig();
        $where = 'a.status =0';

        $alarms_info = $alarms_service->getAlarmsList($where);
        $res = [];
        foreach ($alarms_info['list'] as $value) {
            $res[$value['codeserial']][] = $value;
        }
        $list = [];
        if ($res) {
            foreach ($res as $k => $v) {
                $codeserial = $k;
                $device_info = $devices_service->getInfoByCodeserial($codeserial);
                if (!$device_info) {
                    continue;
                }
                $detail = [];
                foreach ($v as $vv) {
                    $info['id'] = $vv['alarm_id'];
                    $info['alarm_type'] = $alarm_type_config[$vv['type']];
                    $info['created_time'] = date('Y-m-d H:i:s', $vv['created_time']);
                    $info['device_id'] = $device_info['device_id'];
                    $detail[] = $info;
                }
                $r['codeserial'] = $k;
                $r['alarm_times'] = count($v);
                $r['device_address'] = $device_info['device_address'];
                $r['device_category'] = $device_categroy_config[$device_info['device_category']];
                $r['alert_list'] = $detail;
                $list[] = $r;
            }
        }
        if ($list) {
            $this->ajaxReturn(array('code' => 1, 'info' => $list));
        } else {
            $this->ajaxReturn(array('code' => 0, 'info' => $list));
        }

    }

}
